﻿#pragma once

#include "zyBuffGraphics.h"

#pragma comment(lib,"comctl32.lib")
#pragma comment(lib, "Gdiplus.lib")

DarkMoonUI::zyBuffGraphics::zyBuffGraphics()
{

    //初始化GDI+
    //Gdiplus::GdiplusStartupInput StartupInput;
    //StartupInput.GdiplusVersion = 1;
    //StartupInput.DebugEventCallback = NULL;
    //StartupInput.SuppressBackgroundThread = FALSE;
    //StartupInput.SuppressExternalCodecs = FALSE;
    //Gdiplus::GdiplusStartup(&m_pGdiToken, &StartupInput, NULL);
}
DarkMoonUI::zyBuffGraphics::~zyBuffGraphics()
{
   // Gdiplus::GdiplusShutdown(m_pGdiToken);
	Destory();
}

void DarkMoonUI::zyBuffGraphics::Destory(){
    if (m_pGraphics) {
        delete m_pGraphics;
        m_pGraphics = nullptr;
    }
    if (m_hBitmap) {
        ::DeleteObject(m_hBitmap);
        m_hBitmap = nullptr;
    }
    if (m_hdc) {
        ::DeleteDC(m_hdc);
        m_hdc = nullptr;
    }
    m_Width = m_Height = 0;
    m_hasAlpha = false;

}
HDC DarkMoonUI::zyBuffGraphics::gethdc() const { return m_hdc; }
Gdiplus::Graphics* DarkMoonUI::zyBuffGraphics::GetGraphics() const { return m_pGraphics; }
BOOL DarkMoonUI::zyBuffGraphics::Create(int width, int height, HDC hdc, bool useAlpha){
    this->Destory();
    m_hasAlpha = useAlpha;

    // 配置BITMAPINFO
    BITMAPINFO bmi = { 0 };
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = width;
    bmi.bmiHeader.biHeight = -height; // 自顶向下
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = m_hasAlpha ? BI_BITFIELDS : BI_RGB;

    // 设置颜色掩码
    DWORD* masks = nullptr;
    DWORD alphaMasks[3] = { 0x00FF0000, 0x0000FF00, 0x000000FF }; // ARGB
    if (m_hasAlpha) {
        bmi.bmiHeader.biCompression = BI_BITFIELDS;
        masks = alphaMasks;
    }

    // 创建DIB段
    void* pBits = nullptr;
    m_hdc = ::CreateCompatibleDC(hdc);
    m_hBitmap = ::CreateDIBSection(
        m_hdc, &bmi, DIB_RGB_COLORS, &pBits, nullptr, 0
    );

    if (!m_hBitmap) {
        Destory();
        return FALSE;
    }

    // 初始化资源
    ::SelectObject(m_hdc, m_hBitmap);
    m_pGraphics = Gdiplus::Graphics::FromHDC(m_hdc);
    if (!m_pGraphics) {
        Destory();
        return FALSE;
    }

    // 设置高质量渲染
    m_pGraphics->SetCompositingQuality(Gdiplus::CompositingQualityHighSpeed);
    m_pGraphics->SetPixelOffsetMode(Gdiplus::PixelOffsetModeHighSpeed);

    m_Width = width;
    m_Height = height;
    return TRUE;
}
// 带默认参数的创建方法
BOOL DarkMoonUI::zyBuffGraphics::Create(int width, int height, HDC hdc) {
    return Create(width, height, hdc, false); // 默认不启用Alpha
}
BOOL DarkMoonUI::zyBuffGraphics::Copy2dc(HDC hdc, int x, int y, int w, int h, int x1, int y1, UINT rop) const{
	if (!hdc) return FALSE;
	return ::BitBlt(hdc, x, y, w, h, m_hdc, x1, y1, rop);
}
// 带Alpha混合的位块传输
BOOL DarkMoonUI::zyBuffGraphics::Copy2dc(HDC hDest, int x, int y, int w, int h, int xSrc, int ySrc) const {
    if (!hDest || !m_hdc) return FALSE;

    if (m_hasAlpha) {
        BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
        return ::GdiAlphaBlend( hDest, x, y, w, h, m_hdc, xSrc, ySrc, w, h, bf );
    }
    else {
        return ::BitBlt( hDest, x, y, w, h,  m_hdc, xSrc, ySrc, SRCCOPY );
    }
}
// 清空缓冲区（用透明色填充）
void DarkMoonUI::zyBuffGraphics::Clear() {
    if (m_hasAlpha) {
        Gdiplus::SolidBrush brush(Gdiplus::Color(0, 0, 0, 0));
        m_pGraphics->FillRectangle(&brush, 0, 0, m_Width, m_Height);
    }
    else {
        ::PatBlt(m_hdc, 0, 0, m_Width, m_Height, WHITENESS);
    }
}







bool DarkMoonUI::DrawNineGridImage(Graphics* graphics,Image* image,float destX,float destY, float destWidth,float destHeight,
    int gridLeft, int gridRight ,int gridTop , int gridBottom , Unit srcUnit ,  ImageAttributes* imgAttr ) {
    // 默认值处理
    if (gridRight == -1) gridRight = gridLeft;
    if (gridTop == -1) gridTop = gridLeft;
    if (gridBottom == -1) gridBottom = gridLeft;

    // 获取图像尺寸
    const int imageWidth = image->GetWidth();
    const int imageHeight = image->GetHeight();

    // 边界检查
    gridLeft = gridLeft < imageWidth / 2 ? gridLeft : imageWidth / 2;
    gridRight = gridRight < imageWidth / 2 ? gridRight : imageWidth / 2;
    gridTop = gridTop < imageHeight / 2 ? gridTop : imageHeight / 2;
    gridBottom = gridBottom < imageHeight / 2 ? gridBottom : imageHeight / 2;

    // 计算中间区域尺寸
    const int srcMiddleWidth = imageWidth - gridLeft - gridRight;
    const int srcMiddleHeight = imageHeight - gridTop - gridBottom;
    const float destMiddleWidth = destWidth - gridLeft - gridRight;
    const float destMiddleHeight = destHeight - gridTop - gridBottom;

    // 绘制九宫格
    Status status = Ok;

    // 左上
    status = graphics->DrawImage(
        image,
        RectF(destX, destY, gridLeft, gridTop),
        0, 0, gridLeft, gridTop,
        srcUnit, imgAttr
    );

    // 上
    status = graphics->DrawImage(
        image,
        RectF(destX + gridLeft, destY, destMiddleWidth, gridTop),
        gridLeft, 0, srcMiddleWidth, gridTop,
        srcUnit, imgAttr
    );

    // 右上
    status = graphics->DrawImage(
        image,
        RectF(destX + destWidth - gridRight, destY, gridRight, gridTop),
        imageWidth - gridRight, 0, gridRight, gridTop,
        srcUnit, imgAttr
    );

    // 左中
    status = graphics->DrawImage(
        image,
        RectF(destX, destY + gridTop, gridLeft, destMiddleHeight),
        0, gridTop, gridLeft, srcMiddleHeight,
        srcUnit, imgAttr
    );

    // 中
    status = graphics->DrawImage(
        image,
        RectF(destX + gridLeft, destY + gridTop, destMiddleWidth, destMiddleHeight),
        gridLeft, gridTop, srcMiddleWidth, srcMiddleHeight,
        srcUnit, imgAttr
    );

    // 右中
    status = graphics->DrawImage(
        image,
        RectF(destX + destWidth - gridRight, destY + gridTop, gridRight, destMiddleHeight),
        imageWidth - gridRight, gridTop, gridRight, srcMiddleHeight,
        srcUnit, imgAttr
    );

    // 左下
    status = graphics->DrawImage(
        image,
        RectF(destX, destY + destHeight - gridBottom, gridLeft, gridBottom),
        0, imageHeight - gridBottom, gridLeft, gridBottom,
        srcUnit, imgAttr
    );

    // 下
    status = graphics->DrawImage(
        image,
        RectF(destX + gridLeft, destY + destHeight - gridBottom, destMiddleWidth, gridBottom),
        gridLeft, imageHeight - gridBottom, srcMiddleWidth, gridBottom,
        srcUnit, imgAttr
    );

    // 右下
    status = graphics->DrawImage(
        image,
        RectF(destX + destWidth - gridRight, destY + destHeight - gridBottom, gridRight, gridBottom),
        imageWidth - gridRight, imageHeight - gridBottom, gridRight, gridBottom,
        srcUnit, imgAttr
    );

    return status == Ok;
}
void DarkMoonUI::DrawRoundedRectangle( Graphics* graphics,Pen* pen,Brush* brush,float x,float y,
    float width, float height, float cornerRadius) {
    // 确保圆角半径不超过矩形尺寸的一半
    float maxRadius = width < height ? width : height;
    cornerRadius = cornerRadius < maxRadius / 2.0f ? cornerRadius : maxRadius / 2.0f;

    // 定义圆角矩形的路径
    GraphicsPath path;

    // 左上角圆弧
    path.AddArc(x, y, cornerRadius * 2, cornerRadius * 2, 180, 90);

    // 上边线
    path.AddLine(x + cornerRadius, y, x + width - cornerRadius, y);

    // 右上角圆弧
    path.AddArc(x + width - cornerRadius * 2, y, cornerRadius * 2, cornerRadius * 2, 270, 90);

    // 右边线
    path.AddLine(x + width, y + cornerRadius, x + width, y + height - cornerRadius);

    // 右下角圆弧
    path.AddArc(x + width - cornerRadius * 2, y + height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);

    // 下边线
    path.AddLine(x + width - cornerRadius, y + height, x + cornerRadius, y + height);

    // 左下角圆弧
    path.AddArc(x, y + height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);

    // 左边线
    path.AddLine(x, y + height - cornerRadius, x, y + cornerRadius);

    // 闭合路径
    path.CloseFigure();

    // 填充矩形
    if (brush) {
        graphics->FillPath(brush, &path);
    }

    // 绘制边框
    if (pen) {
        graphics->DrawPath(pen, &path);
    }
}